package alg20250217;

public class Text2 {
    public int minOperations(int[] nums, int x) {
        int n = nums.length;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
        }
        int target = sum - x;
        sum = 0;
        int left = 0, right = 0;
        int len = -1;
        while(right < n){
            //进窗口
            sum += nums[right];
            //判断
            while(sum > target){
                //出窗口
                sum -= nums[left++];
            }
            //更新结果
            if(sum == target) len = Math.max(len, right - left + 1);
            right++;
        }
        return len == -1 ? len : n - len;
    }
}
